/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxpocrapi.p                                      */
/* PROCEDURE TYPE        : PO Load CIM-LOAD Process                         */
/* DESCRIPTION           : PO Load CIM-LOAD Process                         */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               01-Jan-24   */
/****************************************************************************/ 
/**   **/
/**   **/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}

{xxjson.i}
{xxcimload.i}

{xxapilog.i}


&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.
// define variable viReturn                     as integer     no-undo.
// define variable ilReturnDataset              as logical     no-undo.
// define variable ilPartialUpdate              as logical     no-undo.
// define variable icPartialUpdateExceptionList as character   no-undo.
// define variable ocPrimaryKey                 as character   no-undo.
// define variable ocRowid                      as character   no-undo.
// define variable oiDraftInstance              as integer     no-undo.
// define variable ocPrimaryKeyName             as character   no-undo.
// define variable vho                          as handle.
// define variable oiReturnStatus               as integer     no-undo.

define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.


define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.
define variable v_existpod            as logical   initial no     no-undo.
define variable v_existpo             as logical   initial no     no-undo.
define variable v_poopen              as logical   initial no     no-undo.
define variable v_podopen             as logical   initial no     no-undo.
define variable v_runproagram         as character                no-undo.
define variable vc_traceid            as character                no-undo.
define variable vi_filecount          as integer                  no-undo.
define variable vc_filename               as character                           no-undo.
define variable vc_topath                 as character                           no-undo.
define variable v_errorcnt                as integer                             no-undo.
define variable vi_reccount                as integer                             no-undo.

define temp-table ttpomstr no-undo

            field po_nbr         like   po_mstr.po_nbr
            field po_vend        like   po_mstr.po_vend 
            field po_site        like   po_mstr.po_site  
            field po_domain      like   po_mstr.po_domain 
            field po_ship        like   po_mstr.po_ship
            field po_bill        like   po_mstr.po_bill  
            field po_ord_date    like   po_mstr.po_ord_date 
            field po_due_date    like   po_mstr.po_due_date
            field po_start       as   date
            field po_expire      as   date
            field po_buyer       like   po_mstr.po_buyer
            field po_contact     like   po_mstr.po_contact
            field po_contract    like   po_mstr.po_contract
            field po_curr        like   po_mstr.po_contract
            field po_rmks        like   po_mstr.po_rmks
            field po_edi         as     logical
            field po_pr_list2    like   po_mstr.po_pr_list2
            field po_pr_list     like   po_mstr.po_pr_list2
            field po_confirm     like   po_mstr.po_confirm
            field po_sched       like   po_mstr.po_sched
            field po_type        like   po_mstr.po_type
            field po_taxable     like   po_mstr.po_taxable
            field po_taxc        like   po_mstr.po_taxc 
            field po_cr_terms    like   po_mstr.po_cr_terms
            field po_update      as  character
            field po_error       as  character
            field po_dataid      as  character
            field po_traceid     as  character
            field po_create_date as date
            field po_cimfile     as character
            field po_logfile     as character
                        
            index ptsk po_dataid  
            index ponbr po_domain po_nbr .


define temp-table ttpoddet no-undo

            field pod_nbr           like   pod_det.pod_nbr
            field pod_domain        like   pod_det.pod_domain
            field pod_line          like   pod_det.pod_line
            field pod_site          like   pod_det.pod_site
            field pod_part          like   pod_det.pod_part
            field pod_qty_ord       like   pod_det.pod_qty_ord
            field pod_um            like   pod_det.pod_um
            field pod_um_conv       like   pod_det.pod_um_conv
            field pod_type          like   pod_det.pod_type
            field pod_acct          like   pod_det.pod_acct
            field pod_cc          like   pod_det.pod_cc
            field pod_sub          like   pod_det.pod_sub
            field pod_project          like   pod_det.pod_project
            field pod_pr_list       like   pod_det.pod_pr_list
            field pod_pur_cost       like   pod_det.pod_pur_cost
            field pod_fix_pr        like   pod_det.pod_fix_pr
            field pod_taxable       like   pod_det.pod_taxable 
            field pod_taxc          like   pod_det.pod_taxc
            field pod_expire        like   pod_det.pod_expire
            field pod_start         as     date
            field pod_wo_lot        like   pod_det.pod_wo_lot
            field pod_op            like   pod_det.pod_op
            field pod_loc           like   pod_det.pod_loc
            field pod_firm_days     like   pod_det.pod_firm_days
            field pod_ord_mult      like   pod_det.pod_ord_mult
            field pod_cum_qty1       as     decimal
            field pod_cum_date1      as     date 
            field pod_fab_days      like   pod_det.pod_fab_days
            field pod_raw_days      like   pod_det.pod_raw_days
            field pod_sftylt_days   like   pod_det.pod_sftylt_days
            field pod_vpart         like   pod_det.pod_vpart
            field pod_sd_pat        like   pod_det.pod_sd_pat
            field pod_sdtCode       like   pod_det.pod_sd_pat
            field pod_translt_days  like   pod_det.pod_translt_days
            field pod_start_eff     like   pod_det.pod_start_eff
            field pod_end_eff       like   pod_det.pod_end_eff
            field pod_plan_days     like   pod_det.pod_plan_days
            field pod_plan_weeks    like   pod_det.pod_plan_weeks
            field pod_plan_mths     like   pod_det.pod_plan_mths
            field pod_error         as     character /* GY01 ADD */
            field pod_dataid        as     character
            field pod_cipid         as     character
                        
            index podnbrline pod_domain pod_nbr pod_line pod_part
            index podtsk   pod_dataid.

   DEFINE DATASET dsPurchaseOrder FOR ttpomstr, ttpoddet
            DATA-RELATION PoNbr FOR ttpomstr, ttpoddet RELATION-FIELDS(po_nbr,pod_nbr).
               
            hdataset = dataset dsPurchaseOrder:HANDLE.             

  
define buffer b_ttpoddet for ttpoddet.

mainloop:
repeat on error undo mainloop, retry mainloop:
   /* Output and Batch Input */
   
   {xxworkpath_e.i &outbound=YES &inbound=yes &archive=YES}
   
   assign
      m_outpath  = m_outpath + "po/"
      m_archpath = m_archpath + "po/"
      m_successpath = m_successpath + "po/"
      m_errorpath = m_errorpath + "po/"
      m_workpath = m_workpath + "po/".
   
 
      empty temp-table ttpoddet.
      empty temp-table ttpomstr.
      v_runproagram = "".

     vc_traceid = "".
   /* Load item master data from JSON  */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttpomstr no-error.
      assign vc_traceid = ttpomstr.po_traceid.
   end.
   
    /* 校验订单号 */
    
    /* 校验行号 */
    /* 校验零件号 */
    /* 校验零件状态 */
    /* 校验订单类型 */
    /* 校验价格单 */
    /* 检查采购控制文件是否锁定 */
    /* 检查订单计量单位与零件基本计量单位是否一样 */
    /* 转包订单的工单号和工序 */
    /* 货币 */
    /* 税率相关 */

   if vc_import and vl_RetOK then 
   do:      
 
   /* scenario 1 order type 1.1 scheduled order or 1.2 discrete order */
   /* scenario 2 new po 1.1 domestic 1.2 import  */
   /* scenario 3 po type 3.1 normal 3.2 subcontract 3.3 memo  */
   /* scenario 4 update po 4.1 po closed 4.2 po open line closed 4.3 po open line open */
   /* I didn't find control field 
   find rqf_ctrl no-lock no-error.
   if available rqf_ctrl then 
   */

   for each ttpomstr where ttpomstr.po_error = "" break by ttpomstr.po_nbr .
    if first-of(ttpomstr.po_nbr) then 
    do:
      v_cim = m_workpath + "PO" + "_" + ttpomstr.po_nbr + "_" + string(ttpomstr.po_traceid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
      v_log = m_workpath + "PO" + "_" + ttpomstr.po_nbr + "_" + string(ttpomstr.po_traceid) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
      output stream f_cim to value(v_cim).
      find po_mstr where po_mstr.po_domain = global_domain
                     and po_mstr.po_nbr = ttpomstr.po_nbr
                     no-lock no-error.
          /* scenario 4 update */
          if available po_mstr then do:
          assign v_existpo = yes.
          if po_stat = "C" or po_stat = "x" then 
          assign v_poopen = no.
          else 
          assign v_poopen = yes.
          end.
          else do:
          assign v_existpo  = no
                 v_poopen = yes.
          end.
           
          /* scheduled order */
          if ttpomstr.po_sched  then 
          do:
          put stream f_cim unformatted v_quote ttpomstr.po_nbr  v_quote v_space       /* po number */
                                       v_quote ttpomstr.po_vend v_quote v_space skip. /* vendor */
          put stream f_cim unformatted /* start date and end date need to assign use interface data */
                                       v_quote ttpomstr.po_start v_quote v_space      /* start effective date */
                                       v_quote ttpomstr.po_expire v_quote v_space     /* end   effective date */
                                       v_quote ttpomstr.po_taxable v_quote v_space    /* taxable  */
                                       v_quote ttpomstr.po_taxc v_quote v_space       /* taxclass  */                                                                   
                                       v_quote ttpomstr.po_cr_terms v_quote v_space   /* credit terms */
                                       v_quote ttpomstr.po_bill v_quote v_space       /* bill-to address */
                                       v_quote ttpomstr.po_ship v_quote v_space       /* ship-to address code*/
                                       v_quote NO v_quote v_space                     /* Print Schedules */
                                       v_quote ttpomstr.po_edi v_quote v_space        /* EDI Schedules */
                                       v_quote NO v_quote v_space                     /* FAX Schedules */
                                       v_quote ttpomstr.po_site v_quote v_space       /*  ap site code*/
                                       v_default v_space                              /*  Zero Schedules */
                                       v_default v_space                              /*  ship via */
                                       v_default v_space                              /*  FOB */
                                       v_quote ttpomstr.po_buyer v_quote v_space      /*  Buyer */
                                       v_quote ttpomstr.po_contact v_quote v_space    /*  contact */
                                       v_quote ttpomstr.po_contract v_quote v_space   /*  contract */
                                       .
        if not v_existpo then                                                           
        put stream f_cim unformatted   v_quote ttpomstr.po_curr v_quote v_space .     /*  CURRENTY */
        put stream f_cim unformatted   v_quote NO v_quote v_space                     /*  Fixed Price */
                                       v_quote 0 v_quote v_space                      /*  Order Revision */
                                       v_default v_space                              /*  Order Rev Date */
                                       v_default v_space                              /*  Print PO */
                                       v_default v_space                              /*  Daybook Set */
                                       v_quote NO v_quote v_space                     /*  Comments */
                                       skip.
          put stream f_cim unformatted v_quote NO v_quote v_space  skip.              /*  Import/Export */                
      if v_existpo then           
          put stream f_cim unformatted v_quote NO v_quote v_space  skip.              /* Is this an order revision change */
          put stream f_cim unformatted v_default skip.                                /* Tax infor */
          put stream f_cim unformatted v_default  v_space                             /* Ship Delivery Pattern Code */        
                                       v_default  skip.                               /* Ship Delivery Time Code */        
         
          end. /* if ttpomstr.po_sched  then  */         
         
          /* discrete order */
         if not ttpomstr.po_sched  then 
         do:
          put stream f_cim unformatted v_quote ttpomstr.po_nbr  v_quote v_space skip  /* po number */
                                       v_quote ttpomstr.po_vend v_quote v_space skip  /* vendor */
                                       v_quote ttpomstr.po_ship v_quote v_space skip  /* Ship-to */
                                       v_quote ttpomstr.po_ord_date v_quote v_space   /* order date */
                                       v_quote ttpomstr.po_due_date v_quote v_space   /* Due date */
                                       v_quote ttpomstr.po_buyer v_quote v_space      /* Buyer    */
                                       v_quote ttpomstr.po_bill v_quote v_space       /* Bill to */
                                       v_default v_space                              /* Sales Job */
                                       v_quote ttpomstr.po_contract v_quote v_space    /* Contract */
                                       v_quote ttpomstr.po_contact v_quote v_space    /* Contact */
                                       v_quote ttpomstr.po_rmks v_quote v_space        /* Remarks */
                                       v_default v_space                              /* Price tbl */
                                       v_quote ttpomstr.po_pr_list v_quote v_space    /* Disc Tbl */
                                       v_default v_space                              /* Ln Disc */
                                       v_quote ttpomstr.po_site    v_quote v_space    /* site*/
                                       v_default v_space                              /* Daybook Set */
                                       v_default v_space                              /* Project */
                                       v_quote   Yes    v_quote v_space               /* Confirming */
                                       v_quote   no     v_quote v_space               /* Imp/exp */
                                       .
        if not v_existpo then                                                            
        put stream f_cim unformatted   v_quote ttpomstr.po_curr v_quote v_space       /* Currency */
                                       v_default v_space   .                          /* LANGUAGE */
        put stream f_cim unformatted   v_quote ttpomstr.po_taxable v_quote v_space    /* Taxable */
                                       v_quote ttpomstr.po_taxc    v_quote v_space    /* Tax Class */
                                       v_default v_space                              /* Tax Date */
                                       v_quote   no     v_quote v_space               /* Fix price */
                                       v_quote ttpomstr.po_cr_terms v_quote v_space   /* Credit Term */
                                       v_default v_space                              /* Credit Term int */
                                       v_default v_space                              /* REQUEST by */
                                       v_quote   no     v_quote v_space               /* Comments */
                                       skip.
         put stream f_cim unformatted  v_default v_space                              /* Tax Usage */
                                       v_default v_space                              /* Tax Environment */
                                       v_default v_space                              /* Tax Class */
                                       v_default v_space                              /* Taxable */
                                       v_quote   no     v_quote v_space               /* Tax in */
                                       skip.
          end. /* if not ttpomstr.po_sched  then  */                            
        end. /* if first-of ttpomstr.po_nbr */ 
       
       /* Scheduled order line */
       if ttpomstr.po_sched then 
       do:
          /* scheduled line information */                                                   
          for each ttpoddet where ttpoddet.pod_nbr = ttpomstr.po_nbr
                              and ttpoddet.pod_domain = global_domain
                              and ttpoddet.pod_site  = po_site
                              and ttpoddet.pod_dataid = ttpomstr.po_dataid.
                                                  
          find pod_det no-lock where   pod_det.pod_domain = global_domain 
                                 and   pod_det.pod_nbr = ttpoddet.pod_nbr
                                 and   pod_det.pod_part = ttpoddet.pod_part
                                 and   pod_det.pod_site = ttpoddet.pod_site
                                 and   pod_det.pod_line = ttpoddet.pod_line
                                 no-error.                                                           
      
          if available pod_det then assign v_existpod = yes. 
          else assign v_existpod = no.
      
          put stream f_cim unformatted v_quote ttpoddet.pod_part v_quote v_space       /*  Item number */                
                                       v_quote ttpoddet.pod_site v_quote v_space       /*  Site        */
                                       v_quote ttpoddet.pod_line  v_quote               /*  line        */
                                       skip.
          /* if new pod line */                                                   
          if not v_existpod then 
          put stream f_cim unformatted v_quote No v_quote v_space  skip.            /*  Copy from  */        
          put stream f_cim unformatted v_quote ttpomstr.po_pr_list v_quote v_space  /*  Discount Table BMPT is supplier code + site*/
                                       v_default  v_space                           /* unit cost */
                                       v_default  v_space                           /* pur account */
                                       v_default  v_space                           /* sub account */
                                       v_default  v_space                           /* cost center */
                                       v_default  v_space                           /* taxable */
                                       v_default  v_space                           /* tax class */
                                       v_quote ttpoddet.pod_type v_quote v_space    /* po type */
                                       v_default  v_space                           /* Item Revision */
                                       v_default  v_space                           /* Item Rev Date */
                                       v_default  v_space                           /* Update Current Cost */
                                       v_quote ttpoddet.pod_loc v_quote  v_space    /* Location */
                                       v_quote NO v_quote v_space                   /* Fixed Price NO */
                                       v_quote ttpoddet.pod_um v_quote v_space      /* Unit of Measure */
                                       skip.
             
         put stream f_cim unformatted v_default v_space skip                        /* Tax infor */
                                      v_quote ttpoddet.pod_um_conv v_quote v_space 
                                       skip.                                         /* UM conversion */
       /* if po type is subcontract */
        if ttpoddet.pod_type = "S" then
        put stream f_cim unformatted v_quote ttpoddet.pod_wo_lot v_quote v_space
                                     v_quote ttpoddet.pod_op v_quote v_space         
                                     skip.

         put stream f_cim unformatted v_quote ttpoddet.pod_firm_days v_quote v_space                    /* Firm Days */
                                      v_quote ttpoddet.pod_ord_mult v_quote v_space                     /* Std Pack Qty */
                                      v_quote ttpoddet.pod_cum_qty1 v_quote v_space                     /* Max Order Qty */
                                      v_quote ttpoddet.pod_cum_date1 v_quote v_space                    /* Cum Start */
                                      v_quote ttpoddet.pod_fab_days v_quote v_space                     /* Fab auth days */
                                      v_quote ttpoddet.pod_raw_days v_quote v_space                     /* Raw Auth Days */
                                      v_quote ttpoddet.pod_sftylt_days v_quote v_space                  /* Safety Days */
                                      v_quote ttpoddet.pod_vpart v_quote v_space                        /* Supplier Item */
                                      v_default v_space                                                 /* Container Item */
                                      v_quote ttpoddet.pod_sd_pat v_quote v_space                       /* Ship Delivery Pattern Code */
                                      v_quote ttpoddet.pod_sdtCode  v_quote v_space                     /* Ship Delivery Time Code */
                                      v_quote ttpoddet.pod_translt_days v_quote v_space                 /* Transport Days */         
                                      v_quote ttpoddet.pod_start  v_quote v_space                       /* Start Effective */
                                      v_quote ttpoddet.pod_expire v_quote v_space                       /* End Effective */
                                       v_quote NO v_quote v_space                    /* Comments */
                                       skip.
                                                                   
         put stream f_cim unformatted  v_quote ttpoddet.pod_plan_days v_quote v_space                            /* Schedule Days */
                                       v_quote ttpoddet.pod_plan_weeks v_quote v_space                            /* Schedule Weeks */
                                       v_quote ttpoddet.pod_plan_mths v_quote v_space                            /* Schedule Months */
                                       skip.
                                                                   
          end. /* for each ttpoddet */
 
       end. /* if ttpomstr.po_sched then  */
        
        /* Discrete Po line detail */
       if not ttpomstr.po_sched then 
       do:
        for each ttpoddet where ttpoddet.pod_nbr = ttpomstr.po_nbr.
                           
                                                  
         find pod_det no-lock where pod_det.pod_domain = global_domain 
                                and pod_det.pod_nbr = ttpoddet.pod_nbr
                                and pod_det.pod_part = ttpoddet.pod_part
                                and pod_det.pod_site = ttpoddet.pod_site
                                and pod_det.pod_line = ttpoddet.pod_line
                               no-error.                                                           
      
         if available pod_det then 
            assign v_existpod = yes. 
         else 
            assign v_existpod = no.                                                                   
         put stream f_cim unformatted  v_quote ttpoddet.pod_line v_quote v_space skip.   /* Po line */
         put stream f_cim unformatted  v_quote ttpoddet.pod_site v_quote v_space skip.   /* Po site */
         if not v_existpod then 
         put stream f_cim unformatted  v_quote ttpoddet.pod_part v_quote v_space skip.   /* Po Part */
         if not v_existpod then
         put stream f_cim unformatted  v_quote ttpoddet.pod_qty_ord v_quote v_space     /* Quantity order */         
                                       v_quote ttpoddet.pod_um v_quote  skip.     /* UM */
                                       
         if v_existpod then
         put stream f_cim unformatted  v_quote ttpoddet.pod_qty_ord v_quote v_space     /* Quantity order */         
                                        skip.     /* UM */
                                                                        
         /* if use price list */
         put stream f_cim unformatted  v_quote ttpoddet.pod_pur_cost v_quote skip.     /* price */               
         put stream f_cim unformatted  v_default v_space                                     /* SINGLE lot */
                                       v_quote ttpoddet.pod_loc v_quote v_space              /* Location */
                                      //  v_default v_space                                     /* location */ /* scp don't give location code */
                                       v_default v_space                                     /* Item Revision */
                                       v_default v_space                                     /* Item Revision date */
                                       v_default v_space                                     /* STATUS */
                                       v_default v_space                                     /* Supplier Item */
                                       .
        if ttpoddet.pod_type = "M" then 
        put stream f_cim unformatted                         
                                       v_default v_space                                     /* comments */.
        put stream f_cim unformatted
                                       v_default v_space                                     /* Due Date */
                                       v_default v_space                                     /* Performance Date */
                                       v_default v_space                                     /* Need date */
                                       v_default v_space                                     /* So job */
                                       v_quote no v_quote v_space .                           /* Fixed Price */
        if ttpoddet.pod_type = "M" then 
        put stream f_cim unformatted   v_quote ttpoddet.pod_acct v_quote v_space                /* Account */
                                       v_quote ttpoddet.pod_sub v_quote v_space                /* Account */
                                       v_quote ttpoddet.pod_cc v_quote v_space                 /* Account */
                                       v_quote ttpoddet.pod_project v_quote v_space .               /* Account */
        if ttpoddet.pod_type <> "M" then
        put stream f_cim unformatted                                       
                                       v_default v_space                                     /* Account */
                                       v_default v_space                                     /* Sub Account */
                                       v_default v_space                                     /* Cost center */
                                       v_default v_space                                     /* Project */
                                       .
        put stream f_cim unformatted   
                                       v_quote ttpoddet.pod_type v_quote v_space             /* Type */
                                       v_quote ttpoddet.pod_taxable v_quote v_space          /* Tax able */
                                       v_default v_space                                     /* Tax Class */
                                       v_default v_space                                     /* Inspect required */
                                       v_quote no v_quote v_space   .                        /* Comment */
        if not v_existpod then 
        put stream f_cim unformatted   v_quote ttpoddet.pod_um_conv v_quote v_space .         /* Conversion */
        put stream f_cim unformatted   v_default v_space                                     /* Update current cost */
                                       skip.
         if ttpoddet.pod_taxable then 
         put stream f_cim unformatted  v_default v_space                                     /* Tax Usage */
                                       v_default v_space                                     /* Tax Environment */
                                       v_default v_space                                     /* Tax Class */
                                       v_default v_space                                     /* Taxable */
                                       v_quote   no     v_quote v_space                      /* Tax in */
                                       skip.                                                                                                                                                                                 
         if ttpoddet.pod_type = "S" then 
         put stream f_cim unformatted  v_default v_space                                     /* Work order */
                                      v_default v_space                                     /* ID */
                                      v_default v_space                                     /* Operation */
                                      v_default v_space                                     /* Subcontract type*/
                                    
                                      skip.                 
          end. /* for each ttpoddet where ttpoddet.pod_nbr = ttpomstr.po_nbr discrete po detail */
       end. /* if not ttpomstr.po_sched then   */
       
        if last-of(ttpomstr.po_nbr) then 
        do:
        
         if not ttpomstr.po_sched then 
         do:
        
          put stream f_cim unformatted  "."      skip.                                        /* SINGLE */                                                       
           put stream f_cim unformatted  "."      skip.                                        /* Tax detail */                                                              
           put stream f_cim unformatted  v_default v_space          skip .                     /* trail screen */           
       if v_existpo then 
           put stream f_cim unformatted  v_default v_space      skip                               
                                         v_quote No v_quote         skip .                     /* Is this an order revision change */  
           if not v_poopen then 
           put stream f_cim unformatted  v_quote No v_quote         skip .                     /* Do you wish to reopen all PO line items */
           put stream f_cim unformatted  "."      skip.                                        /* end */
         end. /*  if not ttpomstr.po_sched then  */
         
         else 
          put stream f_cim unformatted "." skip.        

         output stream f_cim close.
          

         if ttpomstr.po_sched then 
            v_runproagram = "rspomt.p".
         else 
            v_runproagram = "popomt.p".
            
   cimloadprocess:
   do transaction on error undo cimloadprocess, leave cimloadprocess:       
         
         run cimloadprocess(input v_cim,
                            input v_log,
                            input v_runproagram,
                            input vc_traceid,
                            input ttpomstr.po_dataid,
                            input ttpomstr.po_nbr,
                            output v_errstring,
                            output v_errorstatus) .
							
		v_toterrorstatus = v_toterrorstatus or v_errorstatus.  	
   
      if not v_errorstatus then 
      do:
           for each b_ttpoddet where b_ttpoddet.pod_cipid <> "".
                    find pod_det where pod_det.pod_domain = global_domain
                                   and pod_det.pod_nbr  = b_ttpoddet.pod_nbr
                                   and pod_det.pod_line = b_ttpoddet.pod_line
                                   no-error.
                                   
            if available pod_det then 
            assign pod_det.pod__chr10 = b_ttpoddet.pod_cipid.
            
                                   
           end.           
      end.      
           

     /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).         

                                      
                 
       end. /* do vi_filecount = 1 to 2: */   
         
         assign ttpomstr.po_cimfile = replace(v_cim,m_workpath,vc_topath)
                ttpomstr.po_logfile = replace(v_log,m_workpath,vc_topath).

         
             if  v_errorstatus then 
          
          undo cimloadprocess, leave cimloadprocess.      
         
   end. /* do transaction on error undo cimloadprocess, leave cimloadprocess: */
      
           
       end. /*  if last-of(ttpomstr.po_nbr) then  */
       
      end. /* for each ttpomstr */

 end. /* if vc_import and vl_RetOK then  */
 
    for each ttpomstr break by ttpomstr.po_nbr.
 
     if last-of(ttpomstr.po_nbr) then do:
      vi_reccount = vi_reccount + 1.

            
       run CreateAPILogMaster(input vc_traceid,
                              input "createpurchaseorderapi",
                              input "xxpocrapi.p",
                              input ttpomstr.po_cimfile,
                              input ttpomstr.po_logfile,
                              input vc_inputjson,
                              input ttpomstr.po_nbr,
                              input "",
                              input "",
                              input vi_reccount).                            

      end. /* if last-of(ttpomstr.po_nbr) then do: */
      


   
   end. /* for each ttpomstr break by ttpomstr.ttunrcis_billnum. */
   
   
   
   
   
   
 if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
  end.  /* if can-find(first api_status */
         

        /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
 
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
                         vc_topath " " skip 
                         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */      
       
  leave .
end. /* repeat on error undo mainloop, retry mainloop: */



